/**
 * 购物车 - 赠品信息页面
 * @author miaoyl
 * @date 2019/02/12 14:26
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        vueData = {
            GWCQDOCNO:'',
            GWCQ010:''
        },
        callObj,
        moduleName,
        table,listTable,
        layOpenIndex
    ;

    var gwai700GiftEdit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwai700_edit_giftInfo',
                _loadTplsFull: [{
                    dom: 'common_open_gwcq109', tpl: 'common/_open_gwcq109'
                },{
                    dom: 'common_open_gwdcz', tpl: 'common/_open_gwdcz'
                }]
            }, win);
        },
        initListTable: function () {
            //表格初始化
            listTable = table.render({
                elem: app.getByid(doc, 'gwai700_giftInfo_table'),
                id: 'gwai700_giftInfo_table',
                cols: [[
                    {field: 'GWCQZ001', type: 'checkbox', title: '选择'},
                    {field: 'GWCQZ005', width: 140, title: '活动编号'/*, event: 'openGwcq109'*/},
                    {field: 'GWDC003', width: 100, title: '活动名称'},
                    {field: 'GWCQZ002', width: 100, title: '赠品品号'/*, event: 'openGwdcz'*/},
                    {field: 'GWAML003_2', width: 100, title: '赠品品名'},
                    {field: 'GWAML004_2', width: 100, title: '赠品规格'},
                    {field: 'GWCQZ003', width: 80, title: '赠品数量'/*,edit:'text'*/},
                    {field: 'GWCQZ004', width: 80, title: '赠品价格'},
                    {field: 'GWCQZ006', width: 100, title: '单身品号'},
                    {field: 'GWAML003_6', width: 100, title: '单身品名'},
                    {field: 'GWAML004_6', width: 100, title: '单身规格'},
                    {field: 'GWCQ003_SUM', width: 140, title: '订单活动总数量'},
                    {field: 'GWCQ005_SUM', width: 140, title: '订单活动总金额'},
                    {field: 'GWDC020', width: 80, title: '起订数量'},
                    {field: 'GWDC021', width: 80, title: '起订金额'},
                    {field: 'GWDC026', width: 140, title: '最大赠品数量'},
                    {field: 'GWDC025', width: 140, title: '最大赠品金额'},
                    {field: 'GWCQZDOCNO', width: 80, title: '单号',hide:true},
                    {field: 'GWDC005', width: 80, title: '单号',hide:true}
                ]],
                page: {layout: ['prev', 'page', 'next', 'count']}
            });
            //监听单元格编辑
            table.on('edit(gwai700_giftInfo_table)', function(obj){
                if(obj.field == 'GWCQZ003'){
                    var GWCQZ003 =  obj.value;
                    if(obj.data.GWDC005 == '4'){//满量赠送
                        if(obj.data.GWDC026 != '' && obj.data.GWDC026 != null){
                            var max_num = parseInt(obj.data.GWCQ003_SUM / obj.data.GWDC020) * obj.data.GWDC026;
                            if(GWCQZ003 > max_num){
                                obj.updateOfEdit({
                                    'GWCQZ003': ''
                                });
                                app.showWarning("数量过大，超出可赠范围！");
                                return ;
                            }else {
                                obj.updateOfEdit({
                                    'GWCQZ003': GWCQZ003
                                });
                            }
                        }else{
                            var max_Money = parseInt(obj.data.GWCQ003_SUM / obj.data.GWDC020) * obj.data.GWDC025;
                            var money = GWCQZ003 * obj.data.GWCQZ004;
                            if(money > max_Money){
                                obj.updateOfEdit({
                                    'GWCQZ003': ''
                                });
                                app.showWarning("数量过大，超出可赠范围！");
                                return ;
                            }else {
                                obj.updateOfEdit({
                                    'GWCQZ003': GWCQZ003
                                });
                            }
                        }
                    }else if(obj.data.GWDC005 == '3'){//满额赠送
                        if(obj.data.GWDC025 != '' && obj.data.GWDC025 != null){//最大赠送金额
                            var max_Money = parseInt(obj.data.GWCQ005_SUM / obj.data.GWDC021) * obj.data.GWDC025;
                            var money = GWCQZ003 * obj.data.GWCQZ004;
                            if(money > max_Money){
                                obj.updateOfEdit({
                                    'GWCQZ003': ''
                                });
                                app.showWarning("数量过大，超出可赠范围！");
                                return ;
                            }else {
                                obj.updateOfEdit({
                                    'GWCQZ003': GWCQZ003
                                });
                            }
                        }else {//最大赠送数量
                            var max_num = parseInt(obj.data.GWCQ005_SUM / obj.data.GWDC021) * obj.data.GWDC026;
                            if(GWCQZ003 > max_num){
                                obj.updateOfEdit({
                                    'GWCQZ003': ''
                                });
                                app.showWarning("数量过大，超出可赠范围！");
                                return ;
                            }else {
                                obj.updateOfEdit({
                                    'GWCQZ003': GWCQZ003
                                });
                            }
                        }
                    }
                }
            });
            //监听事件
            table.on('tool(gwai700_giftInfo_table)', function(obj){
                switch(obj.event){
                    case 'openGwcq109':
                        self._openTplFull({
                            tag: 'common_open_gwcq109',
                            tagData: {
                                GWCQDOCNO:vueData.GWCQDOCNO,
                                GWCQ010:vueData.GWCQ010,
                                cb:function (data) {
                                    obj.update({
                                        'GWCQZ005': data.GWCQ109,
                                        'GWDC003': data.GWDC003,
                                        'GWCQZ006': data.GWCQ001,
                                        'GWAML003_6': data.GWAML003,
                                        'GWAML004_6': data.GWAML004,
                                        'GWCQ003_SUM': data.GWCQ003_SUM,
                                        'GWCQ005_SUM': data.GWCQ005_SUM,
                                        'GWDC020': data.GWDC020,
                                        'GWDC021': data.GWDC021,
                                        'GWDC025': data.GWDC025,
                                        'GWDC026': data.GWDC026,
                                        'GWDC005': data.GWDC005
                                    });
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择活动',
                                area:['900px', '430px']
                            }
                        });
                        break;
                    case 'openGwdcz' :
                        self._openTplFull({
                            tag: 'common_open_gwdcz',
                            tagData: {
                                GWCQ010:vueData.GWCQ010,//经销商
                                GWDC001:obj.data.GWCQZ005,//活动编号
                                GWDC018:obj.data.GWCQZ006,//产品编号
                                cb: function (data) {
                                    var count = obj.data.GWCQZ001;
                                    var listData = listTable.getDatas();
                                    listData.removeByMap({
                                        GWCQZ001: obj.data.GWCQZ001
                                    });
                                    for(var j=0;j<data.length;j++) {
                                        listData.push({
                                            GWCQZ001:count+j,
                                            GWCQZ005:obj.data.GWCQZ005,
                                            GWDC003:obj.data.GWDC003,
                                            GWCQZ002:data[j].GWDCZ004,
                                            GWAML003_2:data[j].GWAML003,
                                            GWAML004_2:data[j].GWAML004,
                                            GWCQZ003:'',
                                            GWCQZ004:data[j].GWDCZ005,
                                            GWCQZ006:obj.data.GWCQZ006,
                                            GWAML003_6:obj.data.GWAML003_6,
                                            GWAML004_6:obj.data.GWAML004_6,
                                            GWCQ003_SUM:obj.data.GWCQ003_SUM,
                                            GWCQ005_SUM:obj.data.GWCQ005_SUM,
                                            GWDC020:obj.data.GWDC020,
                                            GWDC021:obj.data.GWDC021,
                                            GWDC025:obj.data.GWDC025,
                                            GWDC026:obj.data.GWDC026,
                                            GWCQZDOCNO:vueData.GWCQDOCNO,
                                            GWDC005:obj.data.GWDC005
                                        });
                                        self.tableReload(listData);
                                    }
                                }
                            },
                            index: 1,
                            openOpts: {
                                title: '选择赠品信息',
                                area: ['700px', '430px']
                            }
                        });
                        break;
                };
            });
        },
        tableReload: function (listData) {
            table.reload('gwai700_giftInfo_table', {
                data: listData
            });
        },
        addDataBatch: function () {
            var listData = listTable.getDatas();
            var count = 0;
            if(listData.length > 0){
                count = parseInt(listData[listData.length-1].GWCQZ001);
            }
            listData.push({
                GWCQZ001:count+1,
                GWCQZ005:'',
                GWDC003:'',
                GWCQZ002:'',
                GWAML003_2:'',
                GWAML004_2:'',
                GWCQZ003:'',
                GWCQZ004:'',
                GWCQZ006:'',
                GWAML003_6:'',
                GWAML004_6:'',
                GWCQ003_SUM:'',
                GWCQ005_SUM:'',
                GWDC020:'',
                GWDC021:'',
                GWDC025:'',
                GWDC026:'',
                GWCQZDOCNO:vueData.GWCQDOCNO,
                GWDC005:vueData.GWDC005,
            });
            self.tableReload(listData);
        },
        deleteDataBatch: function(){
            var listData = listTable.getDatas();
            var checkStatus = table.checkStatus('gwai700_giftInfo_table');
            if (checkStatus.data.length > 0) {
                checkStatus.data.forEach(function (v) {
                    listData.removeByMap({
                        GWCQZ001 : v.GWCQZ001
                    });
                });
                self.tableReload(listData);
            }
        },

        addData: function(listData){
            for (var i = 0; i < listData.length; i++) {
                var v = listData[i];
                if (v.GWCQZ003 == '' || v.GWCQZ003 == null
                    || v.GWCSZ005 <=0 ) {
                    app.showWarning('存在赠品数量小于等于0的资料!');
                    return;
                }
                if (v.GWCQZ002 == '' || v.GWCQZ002 == null) {
                    app.showWarning('存在赠品编号为空的资料!');
                    return;
                }
                if (v.GWCQZ005 == '' || v.GWCQZ005 == null) {
                    app.showWarning('存在活动编号为空的资料!');
                    return;
                }
            }
            app.ajax({
                xurl: 'base/gwai700/addGwcqzData',
                xdata: listData,
                loading: true,
                toJsonStr: true,
                okmsg: moduleName +'增加成功',
                cb: function (result) {
                    if (result.ok) {
                        self.closePage();
                    }
                }
            });
        },
        closePage: function(){
            layer.close(layOpenIndex);
        },
        _call: function (tagData) {
            moduleName = tagData.moduleName;
            layOpenIndex = tagData.layOpenIndex;
            vueData.GWCQDOCNO = tagData.GWCQDOCNO;
            vueData.GWCQ010 = tagData.GWCQ010;
            app.ajax({
                xurl: 'base/gwai700/getGiftInfoData',
                xdata: {
                    GWCQDOCNO : tagData.GWCQDOCNO,
                    GWCQ010 : tagData.GWCQ010
                },
                cb: function (result) {
                    if (result.ok) {
                        self.tableReload(result.data.data);
                    }
                }
            });

        },
        _ready: function () {
            table = layui.table;
            var vue = new Vue({
                el: '#gwai700_edit_giftInfo',
                data: '',
                methods: {
                    addDataBatch: function () {
                        self.addDataBatch();
                    },
                    deleteDataBatch: function () {
                        self.deleteDataBatch();
                    },
                    sure: function () {
                        var listData = listTable.getDatas();
                        listData.addMap(vueData.form,true);
                        self.addData(listData);
                    },
                    cancel: function () {
                        self.closePage();
                    }
                },
                watch: {},
                created: function () {
                    self._setEleIsReady('gwai700_edit_giftInfo');
                }
            });
            self.initListTable();
        },
        _lazy: function () {
            this._super();
        }
    });

    return new gwai700GiftEdit();

});